//
//  VideoRecordViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "VideoRecordViewController.h"
#import "VideoRecordModel.h"

@interface VideoRecordViewController (){
    TXLivePlayer *_txLivePlayer;
    UIView *_playView;
    NSMutableArray *_videoArray;
}
@end

@implementation VideoRecordViewController

#pragma mark -- 获取回放地址
- (void)VideoAddressRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"channel":self.channelId
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kVideoRecord];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"transcodeList"];
            for (NSDictionary *RecordDict in array) {
                VideoRecordModel *model = [[VideoRecordModel alloc] initWithDict:RecordDict];
                [_videoArray addObject:model];
            }
            [self startVideoRecord];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"直播回放";
    [self setUpUI];
    [self VideoAddressRequest];
}

- (void)setUpUI {
    _videoArray = [NSMutableArray arrayWithCapacity:1];
    _txLivePlayer = [[TXLivePlayer alloc] init];

}

- (void)startVideoRecord {
    _playView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_playView];
    [_txLivePlayer setupVideoWidget:[UIScreen mainScreen].bounds containView:_playView insertIndex:0];
    VideoRecordModel *model = _videoArray[1];
    NSString* flvUrl = model.url;
    //用 setupVideoWidget 给播放器绑定决定渲染区域的view，其首个参数 frame 在 1.5.2 版本后已经废弃
    [_txLivePlayer setupVideoWidget:CGRectMake(0, 0, 0, 0) containView:_playView insertIndex:0];
    //使用 startPlay 即可开始播放，如果是直播的话我们推荐 FLV 协议
    [_txLivePlayer startPlay:flvUrl type:PLAY_TYPE_VOD_HLS];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
